﻿//===============================================================================
// Microsoft patterns & practices Enterprise Library Contribution
// Query Application Block
//===============================================================================

using System.Collections.Generic;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration.Manageability;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration.Manageability.Adm;
using EntLibContrib.Query.Properties;

namespace EntLibContrib.Query.Configuration.Manageability.Parameters
{
	/// <summary>
	/// Provides an implementation for <see cref="XmlParameterData"/> that
	/// splits policy overrides processing and WMI objects generation, performing appropriate logging of 
	/// policy processing errors.
	/// </summary>
	public class XmlParameterDataManageabilityProvider
		: ParameterDataManageabilityProvider<XmlParameterData>
	{
		#region Constants
		/// <summary>
		/// The name of the parameter namespace property.
		/// </summary>
		public const string ParameterNamespacePropertyName = "parameterNamespace";
		#endregion

		#region Construction
		/// <summary>
		/// Initialize a new instance of the <see cref="XmlParameterDataManageabilityProvider"/> class.
		/// </summary>
		public XmlParameterDataManageabilityProvider()
		{
			XmlParameterDataWmiMapper.RegisterWmiTypes();
		}
		#endregion

		#region ADM Generation
		/// <summary>
		/// Adds the ADM parts that represent the properties of
		/// a specific instance of the configuration element type managed by the receiver.
		/// </summary>
		/// <param name="contentBuilder">The <see cref="AdmContentBuilder"/> to which the Adm instructions are to be appended.</param>
		/// <param name="configurationObject">The configuration object instance.</param>
		/// <param name="configurationSource">The configuration source from where to get additional configuration
		/// information, if necessary.</param>
		/// <param name="elementPolicyKeyName">The key for the element's policies.</param>
		/// <remarks>
		/// Subclasses managing objects that must not create a policy will likely need to include the elements' keys when creating the parts.
		/// </remarks>
		protected override void AddElementAdministrativeTemplateParts(AdmContentBuilder contentBuilder,
																																	XmlParameterData configurationObject,
																																	IConfigurationSource configurationSource,
																																	string elementPolicyKeyName)
		{
			contentBuilder.AddEditTextPart(InternalResources.XmlParameterDataNamespacePartName,
																		elementPolicyKeyName,
																		ParameterNamespacePropertyName,
																		configurationObject.Namespace,
																		255,
																		false);
		}
		#endregion

		#region Manageability Support
		/// <summary>
		/// Overrides the <paramref name="configurationObject"/>'s properties with the Group Policy values from the
		/// registry.
		/// </summary>
		/// <param name="configurationObject">The configuration object for instances that must be managed.</param>
		/// <param name="policyKey">The <see cref="IRegistryKey"/> which holds the Group Policy overrides for the
		/// configuration element.</param>
		/// <remarks>Subclasses implementing this method must retrieve all the override values from the registry
		/// before making modifications to the <paramref name="configurationObject"/> so any error retrieving
		/// the override values will cancel policy processing.</remarks>
		protected override void OverrideWithGroupPolicies(XmlParameterData configurationObject,
																											IRegistryKey policyKey)
		{
			string namespaceOverride = policyKey.GetStringValue(ParameterNamespacePropertyName);

			configurationObject.Namespace = namespaceOverride;
		}

		/// <summary>
		/// Creates the <see cref="ConfigurationSetting"/> instances that describe the 
		/// configurationObject.
		/// </summary>
		/// <param name="configurationObject">The configuration object for instances that must be managed.</param>
		/// <param name="wmiSettings">A collection to where the generated WMI objects are to be added.</param>
		protected override void GenerateWmiObjects(XmlParameterData configurationObject,
																							 ICollection<ConfigurationSetting> wmiSettings)
		{
			XmlParameterDataWmiMapper.GenerateWmiObjects(configurationObject, wmiSettings);
		}
		#endregion
	}
}
